package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.OmcServiceThirdLevelMapper;
import com.csun.cmny.provider.exceptions.OmcBizException;
import com.csun.cmny.provider.model.domain.OmcServiceSecondLevel;
import com.csun.cmny.provider.model.domain.OmcServiceThirdLevel;
import com.csun.cmny.provider.model.dto.ServiceThirdLevelQueryDto;
import com.csun.cmny.provider.model.vo.ServiceThirdLevelVO;
import com.csun.cmny.provider.service.OmcServiceThirdLevelService;
import com.csun.cmny.provider.vo.OrderVo;
import com.csun.cmny.util.PublicUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
@Transactional
public class OmcServiceThirdLevelServiceImpl extends BaseService<OmcServiceThirdLevel> implements OmcServiceThirdLevelService {

    @Resource
    private OmcServiceThirdLevelMapper omcServiceThirdLevelMapper;

    @Override
    public Integer saveServiceThirdLevel(OmcServiceThirdLevel omcServiceThirdLevel, LoginAuthDto loginAuthDto) {
        //校验名称
        checkNameExsit(omcServiceThirdLevel);

        if (PublicUtil.isEmpty(omcServiceThirdLevel.getId())) {
            return omcServiceThirdLevelMapper.insertSelective(omcServiceThirdLevel);
        }else {
            return omcServiceThirdLevelMapper.updateByPrimaryKeySelective(omcServiceThirdLevel);
        }
    }

    @Override
    public int deleteThirdLevel(Long thirdLevelId) {
        return omcServiceThirdLevelMapper.deleteByPrimaryKey(thirdLevelId);
    }

    @Override
    public PageInfo listPage(ServiceThirdLevelQueryDto serviceThirdLevelQueryDto) {

        PageHelper.startPage(serviceThirdLevelQueryDto.getPageNum(), serviceThirdLevelQueryDto.getPageSize());
        List<ServiceThirdLevelVO> voList = omcServiceThirdLevelMapper.listPage(serviceThirdLevelQueryDto);
        return new PageInfo<>(voList);
    }

    private void checkNameExsit(OmcServiceThirdLevel omcServiceThirdLevel) {
        Example example = new Example(OmcServiceSecondLevel.class);
        Example.Criteria criteria = example.createCriteria();
        if(PublicUtil.isNotEmpty(omcServiceThirdLevel.getId())){
            criteria.andNotEqualTo("id",omcServiceThirdLevel.getId());
        }
        criteria.andEqualTo("name",omcServiceThirdLevel.getName());
        int i = selectCountByExample(example);
        if(i > 0){
            throw new OmcBizException(ErrorCodeEnum.OMC10080004);
        }
    }
}
